package com.lagou.cl.lisenter;

import com.alibaba.fastjson.JSON;
import com.lagou.cl.pojo.PayOrder;
import com.lagou.cl.service.PayOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 程  林
 * @date 2020-10-12 01:37
 * @description //
 * @since V1.0.0
 */
@Component
public class PayOrderTimeOutLisenter {

	private final PayOrderService payOrderService;

	public PayOrderTimeOutLisenter(PayOrderService payOrderService) {
		this.payOrderService = payOrderService;
	}

	@RabbitListener(queues = "queue.pay.dlx")
	public void onMessage(Message message, Channel channel) {

		String order = new String(message.getBody());
		System.out.println("接受到死信队列的消息: " + order);

		PayOrder payOrder = JSON.parseObject(order, PayOrder.class);

		PayOrder payOrder1 = payOrderService.queryPayOrderByOrderId(payOrder.getOrderId());
		// 订单状态不等于2（已支付），说明已超时
		if (payOrder1 != null && !payOrder1.getState().equals(2)) {
			payOrder1.setState(3);
			payOrderService.updateOrder(payOrder1);
		}

	}
}
